<template>
  <div class="app-container">
    <!-- 面包屑导航 -->
    <breadcrumb />

    <!-- 搜索区域 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <!-- 退货单号 -->
      <el-form-item label="退货单号" prop="returnNo">
        <el-input v-model="queryParams.returnNo" placeholder="请输入退货单号" clearable size="mini" />
      </el-form-item>
      <!-- 原采购单号 -->
      <el-form-item label="原采购单号" prop="inboundNo">
        <el-input v-model="queryParams.inboundNo" placeholder="请输入原采购单号" clearable size="mini" />
      </el-form-item>
      <!-- 供应商 -->
      <el-form-item label="供应商" prop="supplierName">
        <el-input v-model="queryParams.supplierName" placeholder="请输入供应商名称" clearable size="mini" />
      </el-form-item>
      <!-- 退货状态 -->
      <el-form-item label="退货状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择退货状态" clearable size="mini">
          <el-option v-for="item in dict.audit_status" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <!-- 退货日期 -->
      <el-form-item label="退货日期" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" type="daterange" range-separator="至"
                        start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" size="mini" />
      </el-form-item>
      <!-- 搜索/重置按钮 -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据表格 -->
    <el-table v-loading="loading" :data="tableData" border stripe style="width: 100%">
      <el-table-column label="序号" type="index" width="50" align="center" />
      <el-table-column label="退货单号" prop="returnNo" align="center" width="140" />
      <el-table-column label="原采购单号" prop="originalInboundNo" align="center" width="140" />
      <el-table-column label="供应商名称" prop="supplierName" align="center" width="120" />
      <el-table-column label="退货状态" prop="auditStatus" align="center" width="100">
        <template slot-scope="scope">
          <el-tag :type="getStatusTagType(scope.row.auditStatus)">
            {{ getStatusTagLabel(scope.row.auditStatus) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="退货日期" prop="createTime" align="center" width="140" />
      <el-table-column label="操作人ID" prop="approver" align="center" width="100" />
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 查看退货单弹窗（新增完整代码） -->
    <el-dialog title="退货单详情" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="viewForm" :model="form" label-width="120px">
        <el-form-item label="退货单号" prop="returnNo">
          <el-input v-model="form.returnNo" disabled/>
        </el-form-item>
        <el-form-item label="采购入库单号" prop="originalInboundNo">
          <el-input v-model="form.originalInboundNo" disabled/>
        </el-form-item>>
        <el-form-item label="供应商编号" prop="supplierId">
          <el-input v-model="form.supplierId" disabled/>
        </el-form-item>
        <el-form-item label="供应商名称" prop="supplierName">
          <el-input v-model="form.supplierName" disabled/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">关闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listReturn, getReturn, addReturn, updateReturn } from "@/api/erp/returns";
import { getDicts } from "@/api/system/dict/data";
import Breadcrumb from "@/components/Breadcrumb/index.vue";
import Pagination from "@/components/Pagination/index.vue";

export default {
  name: "PurchaseReturn",
  components: { Breadcrumb, Pagination },
  data() {
    return {
      // 表格数据
      tableData: [],
      total: 0,
      loading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        returnNo: "",
        originalInboundNo: "",
        supplierName: "",
        returnStatus: "",
        returnTimeRange: []
      },
      // 弹窗控制
      open: false,
      title: "",
      // 表单数据
      form: {
        id: null,
        returnNo: "",
        inboundNo: "",
        supplierId: "",
        supplierName: "",
        returnStatus: "0", // 0-待审核 1-已审核 2-已拒绝
        returnTime: "",
        returnReason: "",
        returnItems: [] // 退货明细列表
      },
      // 字典数据（退货状态）
      dict: {
        return_status: []
      }
    };
  },
  created() {
    this.getDicts("return_status").then(() => {
      this.getList();
    });
  },
  methods: {
    // 获取字典数据
    getDicts(dictType) {
      return new Promise((resolve) => {
        getDicts(dictType).then((response) => {
          this.dict[dictType] = response.data;
          resolve();
        });
      });
    },
    // 获取列表数据
    getList() {
      this.loading = true;
      listReturn(this.queryParams).then(response => {
        this.tableData = response.rows;
        console.log(this.tableData)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置
    resetQuery() {
      this.$refs.queryForm.resetFields();
      this.queryParams.returnTimeRange = [];
      this.handleQuery();
    },
    // 查看详情
    handleView(row) {
      this.title = "查看退货单";
      getReturn(row.returnNo).then(response => {
        console.log(response.data)
        this.form = response.data || {};
        this.open = true;
      });
    },
    // 编辑
    handleEdit(row) {
      this.title = "编辑退货单";
      getReturn(row.id).then(response => {
        this.form = response;
        this.open = true;
      });
    },
    // 提交表单
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const api = this.form.id ? updateReturn : addReturn;
          api(this.form).then(() => {
            this.$message.success("操作成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    // 状态标签类型
    getStatusTagType(status) {
      switch (status) {
        case "0": return "warning"; // 待审核
        case "1": return "success"; // 已审核
        case "2": return "danger";  // 已拒绝
        default: return "info";
      }
    },
    // 状态标签文本
    getStatusTagLabel(status) {
      const item = this.dict.return_status.find(i => i.value === status);
      return item ? item.label : "未知";
    },

  }
};
</script>
